# Replication Archive for: Schwarz, Susanne and Alexander Coppock. (2020) 
# "What Have We Learned About Gender From Candidate Choice Experiments? 
# A Meta-analysis of 67 Factorial Survey Experiments" 
# Forthcoming, Journal of Politics.

rm(list = ls())
library(tidyverse)
library(rmeta)
library(estimatr)
library(xtable)

source("helpers.R")
sate_estimates <- read_rds("sate_estimates.rds")



# Table 1 -----------------------------------------------------------------

# Totals Row
totals_row <- 
sate_estimates %>%
  summarise(
    study_name_short = "Total",
    n_subjects = sum(n_subjects),
    n_subjects = formatC(n_subjects, format = "d", big.mark = ","),
    n_ratings = sum(n_ratings),
    n_ratings = formatC(n_ratings, format = "d", big.mark = ","),
    have_data = sum(have_data == "Yes"),
    sample_type = ""
  ) 


# totals_row %>%
#   xtable(digits = 0) %>%
#   print.xtable(
#     include.rownames = FALSE,
#     include.colnames = FALSE,
#     hline.after = c(),
#     only.contents = TRUE,
#     file = "totals_row.tex"
#   )

table_df <- 
sate_estimates %>%
  transmute(
    study_name_short,
    n_subjects = formatC(n_subjects, format = "d", big.mark = ","),
    n_ratings = formatC(n_ratings, format = "d", big.mark = ","),
    have_data,
    sample_type
  ) %>%
  arrange(desc(have_data), study_name_short) 


# table_df %>%
#   xtable() %>%
#   print.xtable(
#     include.rownames = FALSE,
#     include.colnames = FALSE,
#     hline.after = c(),
#     only.contents = TRUE,
#     file = "studies.tex"
#   )


# Figure 1 ----------------------------------------------------------------

summary_df <-
  sate_estimates %>%
  do(tidy(with(
    ., meta.summaries(d = estimate, se = std.error, method = "random")
  ))) %>%
  mutate(
    study_name_short = "Random-effects meta-analysis",
    estimate_100 = estimate * 100,
    std.error_100 = std.error * 100,
    conf.high_100 = conf.high * 100,
    conf.low_100 = conf.low * 100
  )

gg_df <-
  sate_estimates %>%
  bind_rows(summary_df) %>%
  mutate(
    facet = as.numeric(study_name_short == "Random-effects meta-analysis"),
    study_name_short = fct_reorder(factor(study_name_short), estimate),
    estimate_100 = estimate * 100,
    std.error_100 = std.error * 100,
    conf.high_100 = conf.high * 100,
    conf.low_100 = conf.low * 100,
    se_entry = make_se_entry(estimate_100, std.error_100, digits = 1)
  )



g <-
  ggplot(gg_df, aes(x = estimate_100, y = study_name_short)) +
  geom_point(alpha = 0.8, stroke = 0) +
  geom_errorbarh(aes(xmin = conf.low_100, xmax = conf.high_100),
                 height = 0,
                 alpha = 0.5) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  geom_vline(data = summary_df, aes(xintercept = estimate_100)) +
  geom_rect(
    data = summary_df,
    aes(
      xmin = conf.low_100,
      xmax = conf.high_100,
      x = NULL,
      y = NULL
    ),
    ymin = 0,
    ymax = 100,
    alpha = .1
  ) +
  geom_text(
    x = 20,
    aes(y = study_name_short, label = se_entry),
    hjust = 1,
    size = 2.5,
    nudge_y = 0.25
  ) +
  coord_cartesian(xlim = c(-20, 20)) +
  xlab("ATE estimate in percentage points") +
  facet_grid(rows = vars(facet),
             scales = "free_y",
             space = "free") +
  theme_bw() +
  theme(
    axis.title.y = element_blank(),
    axis.title.x = element_text(size = 8),
    axis.text.y = element_text(size = 8),
    legend.position = "bottom",
    strip.background = element_blank(),
    strip.text = element_blank()
  )

g

# ggsave(
#   "meta_sates_graph.pdf",
#   g,
#   height = 7.5,
#   width = 6.5
# )


# In text numbers ---------------------------------------------------------

sate_estimates %>%
  summarise(
    pos_and_sig = sum(conf.low > 0),
    neg_and_sig = sum(conf.high < 0),
    not_sig = sum(conf.low < 0 & conf.high > 0),
    pos = sum(estimate > 0),
    mean_pos = mean(estimate > 0),
    neg = sum(estimate < 0)
  )


# Table 2 -----------------------------------------------------------------

modern_df <-
  sate_estimates %>%
  mutate(group = if_else(year > 2010, "Post-2014 studies", "Pre-1998 studies")) %>%
  group_by(group) %>%
  do(tidy(with(
    ., meta.summaries(d = estimate, se = std.error, method = "random")
  )))

representative_df <-
  sate_estimates %>%
  mutate(
    group = if_else(
      representative_sample == 1,
      "Representative sample",
      "Convenience/student sample"
    )
  ) %>%
  group_by(group) %>%
  do(tidy(with(
    ., meta.summaries(d = estimate, se = std.error, method = "random")
  )))

USA_df <-
  sate_estimates %>%
  mutate(group = if_else(country == "USA", "American sample", "Non-American sample")) %>%
  group_by(group) %>%
  do(tidy(with(
    ., meta.summaries(d = estimate, se = std.error, method = "random")
  )))

trimmed_df <-
  sate_estimates %>%
  filter(estimate < quantile(estimate, 0.95),
         estimate > quantile(estimate, 0.02),
  ) %>%
  do(tidy(with(
    ., meta.summaries(d = estimate, se = std.error, method = "random")
  ))) %>%
  mutate(group = "Trimmed (middle 90% of estimates)")

all_df <-
  sate_estimates %>%
  do(tidy(with(
    ., meta.summaries(d = estimate, se = std.error, method = "random")
  ))) %>%
  mutate(group = "All studies")

robustness_df <-
  bind_rows(modern_df,
            representative_df,
            USA_df,
            trimmed_df,
            all_df) %>%
  transmute(
    group,
    entry = make_se_entry(estimate, std.error, 3),
    interval = make_interval_entry(conf.low, conf.high, 3),
    n_studies
  )

# robustness_df %>%
#   xtable %>%
#   print.xtable(
#     include.colnames = FALSE,
#     include.rownames = FALSE,
#     only.contents = TRUE,
#     hline.after = c(),
#     file = "robustness.tex"
#   )
